package com.huizhonglm.diap.widget.shortcuts;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.utils.DisplayUtil;
import com.huizhonglm.diap.model.Shortcut;

import java.util.ArrayList;
import java.util.List;

/**
 * 带表情的自定义输入框
 * 
 * @author LiXiaolong
 * @since 2014年9月24日下午1:13:11
 * @version 1.0
 */
public class ShortcutsLayout extends RelativeLayout implements OnItemClickListener {

	private Context context;

	/** 表情页的监听事件 */
	// private OnCorpusSelectedListener mListener;

	/** 显示表情页的viewpager */
	private ViewPager viewPager;

	/** 表情页界面集合 */
	private ArrayList<View> pageViews;

	/** 游标显示布局 */
	private LinearLayout pointLayout;

	/** 游标点集合 */
	private ArrayList<ImageView> pointViews;

	/** 表情集合 */
	private List<List<Shortcut>> pageList;

	/** 表情数据填充器 */
	private List<ShortcutsGridAdapter> gridAdapters;

	/** 当前表情页 */
	private int current = 0;

	/** 每一页表情的个数 */
	private int pageSize = 8;

	private OnShortcutItemClickListener onItemClick;

	public ShortcutsLayout(Context context) {
		super(context);
		this.context = context;
	}

	public ShortcutsLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	public ShortcutsLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		// pageList = parseData(context);
		// onCreate();
	}

	public void onCreate(List<Shortcut> data) {
		pageList = parseData(data);
		initViews();
		initViewPager();
		initPoint();
		initData();
	}

	private List<List<Shortcut>> parseData(List<Shortcut> data){
		List<List<Shortcut>> pageData = new ArrayList<>();
		int pageCount = data.size() / pageSize;
		if (data.size() % pageSize != 0) {
			pageCount = (int) Math.ceil(data.size() / pageSize + 0.1);
		}
		for (int i = 0; i < pageCount; i++) {
			pageData.add(getData(data, i));
		}
		return pageData;
	}

	/**
	 * 初始化控件
	 */
	private void initViews() {
		viewPager = (ViewPager) findViewById(R.id.grid_pager_vp);
		pointLayout = (LinearLayout) findViewById(R.id.point_layout);
	}

	/**
	 * 初始化显示表情的viewpager
	 */
	private void initViewPager() {
		pageViews = new ArrayList<>();
		gridAdapters = new ArrayList<>();
		for (List<Shortcut> entityList : pageList) {
			GridView grid = new GridView(context);
			ShortcutsGridAdapter adapter = new ShortcutsGridAdapter(context, entityList);
			grid.setAdapter(adapter);
			gridAdapters.add(adapter);
			grid.setOnItemClickListener(this);
			grid.setNumColumns(4);
			grid.setBackgroundResource(R.drawable.bg_transparent);
			grid.setHorizontalSpacing(0);
			grid.setVerticalSpacing(0);
			grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
			grid.setCacheColorHint(0);
			grid.setSelector(new ColorDrawable(Color.TRANSPARENT));
			grid.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			grid.setGravity(Gravity.CENTER);
			pageViews.add(grid);
		}
	}

	/**
	 * 初始化游标
	 */
	private void initPoint() {
		if (pageViews.size() <= 1) {
			pointLayout.setVisibility(View.GONE);
		} else {
			pointLayout.setVisibility(View.VISIBLE);
		}
		pointViews = new ArrayList<>();
		ImageView point;
		int dp_6 = DisplayUtil.dip2px(6f);
		for (int i = 0; i < pageViews.size(); i++) {
			point = new ImageView(context);
			point.setBackgroundResource(R.drawable.point_grid_pager_n);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(dp_6, dp_6);
			lp.gravity = Gravity.RIGHT;
			lp.leftMargin = dp_6;
			lp.rightMargin = dp_6;
			pointLayout.addView(point, lp);
			if (i == 0) {
				point.setBackgroundResource(R.drawable.point_grid_pager_s);
			}
			pointViews.add(point);
		}
	}

	/**
	 * 填充数据
	 */
	private void initData() {
		current = 0;
		viewPager.setAdapter(new ShortcutsPagerAdapter(pageViews));
		viewPager.setCurrentItem(current);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				current = position;
				// 描绘分页点
				drawPoint(position);
				viewPager.setCurrentItem(position);

//				// 如果是第一屏或者是最后一屏禁止滑动，其实这里实现的是如果滑动的是第一屏则跳转至第二屏，如果是最后一屏则跳转到倒数第二屏.
//				if (arg0 == pointViews.size() || arg0 == 0) {
//					if (arg0 == 0) {
//						viewPager.setCurrentItem(arg0);// 第二屏 会再次实现该回调方法实现跳转.
//						pointViews.get(1).setBackgroundResource(R.drawable.point_grid_pager_s);
//					} else {
//						viewPager.setCurrentItem(arg0 - 1);// 倒数第二屏
//						pointViews.get(arg0 - 1).setBackgroundResource(R.drawable.point_grid_pager_s);
//					}
//				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}

//	private List<List<Shortcut>> parseData(Context context) {
//		List<String> data = new ArrayList<>();
//		String[] resArray = context.getResources().getStringArray(R.array.grid_pager);
//		if (EmptyUtil.isEmpty(resArray)) {
//			return null;
//		}
//        Collections.addAll(data, resArray);
//		List<List<Shortcut>> pageList = new ArrayList<>();
//		List<Shortcut> entityList = new ArrayList<>();
//		Shortcut entity;
//		try {
//			for (String str : data) {
//				String[] text = str.split(",");
//				String resName = text[0].substring(0, text[0].lastIndexOf("."));
//				int drawableID = context.getResources().getIdentifier(resName, "drawable", context.getPackageName());
//				if (drawableID != 0) {
//					entity = new Shortcut();
//					entity.setDrawable(drawableID);
//					entity.setName(text[1]);
//					entityList.add(entity);
//				}
//			}
//
//			int pageCount = entityList.size() / pageSize;
//			if (entityList.size() % pageSize != 0) {
//				pageCount = (int) Math.ceil(entityList.size() / pageSize + 0.1);
//			}
//			for (int i = 0; i < pageCount; i++) {
//				pageList.add(getData(entityList, i));
//			}
//			return pageList;
//		} catch (Exception e) {
//			e.printStackTrace();
//			return null;
//		}
//	}

	private List<Shortcut> getData(List<Shortcut> entityList, int page) {
		int start = page * pageSize;
		int end = start + pageSize;
		if (end > entityList.size()) {
			end = entityList.size();
		}
		List<Shortcut> list = new ArrayList<>();
		list.addAll(entityList.subList(start, end));
		return list;
	}

	/**
	 * 绘制游标背景
	 */
	public void drawPoint(int index) {
		for (int i = 0; i < pointViews.size(); i++) {
			if (index == i) {
				pointViews.get(i).setBackgroundResource(R.drawable.point_grid_pager_s);
			} else {
				pointViews.get(i).setBackgroundResource(R.drawable.point_grid_pager_n);
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		Shortcut shortcut = (Shortcut) gridAdapters.get(current).getItem(position);
		if (onItemClick != null) {
			onItemClick.onShortcutItemClick(shortcut);
		}
	}

	public void setOnShortcutItemClickListener(OnShortcutItemClickListener listener){
		this.onItemClick = listener;
	}

	public interface OnShortcutItemClickListener {
		void onShortcutItemClick(Shortcut entity);
	}

}
